package com.wgts.wgts.mvp.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerWgtsResetPasswordActivityComponent;
import com.wgts.wgts.dagger.module.WgtsResetPasswordActivityModule;
import com.wgts.wgts.mvp.presenter.IWgtsResetPasswordActivityPresenter;
import com.wgts.wgts.mvp.view.IWgtsResetPasswordActivityView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *   author:handsome
 *   data:2018/11/717:27
 *   packeage:${PAKAGE_NAME}
 *
 */public class WgtsResetPasswordActivity extends BaseActivity implements IWgtsResetPasswordActivityView {
    @Inject
    IWgtsResetPasswordActivityPresenter presenter;
    @BindView(R.id.back_image)
    ImageView backImage;
    @BindView(R.id.password_et)
    EditText passwordEt;
    @BindView(R.id.password_ly)
    RelativeLayout passwordLy;
    @BindView(R.id.password_again_et)
    EditText passwordAgainEt;
    @BindView(R.id.password_again_ly)
    RelativeLayout passwordAgainLy;
    @BindView(R.id.affirm_ly)
    RelativeLayout affirmLy;

    public static final String PHONE = "phone";
    public static final String CODE = "code";
    private String phone, code;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wgts_reset_password_activity);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        phone = intent.getStringExtra(PHONE);
        code = intent.getStringExtra(CODE);
    }

    @Override
    public void init() {
        DaggerWgtsResetPasswordActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .wgtsResetPasswordActivityModule(new WgtsResetPasswordActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void resetPassword() {
        MoHuToast.showToast("设置成功");
        Intent intent = new Intent(WgtsResetPasswordActivity.this, LoginActivity.class);
        intent.putExtra(LoginActivity.PHONE, phone);
        intent.putExtra(LoginActivity.PASSWORD, passwordAgainEt.getText().toString().trim());
        startActivity(intent);
        finish();
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
    }

    @OnClick({R.id.back_image, R.id.affirm_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_image:
                finish();
                break;
            case R.id.affirm_ly:
                reset();
                break;
        }
    }

    private void reset(){
        if(passwordEt.getText().toString().trim().length() < 6 || passwordEt.getText().toString().trim().length() > 16){
            MoHuToast.showToast("请输入6-16位密码");
            return;
        }

        if(!passwordAgainEt.getText().toString().trim().equals(passwordEt.getText().toString().trim())){
            MoHuToast.showToast("两次输入密码不一致");
            return;
        }

        presenter.resetPassword(passwordEt.getText().toString().trim(), passwordAgainEt.getText().toString().trim()
        , code, phone);
    }
}
